
let index = 0;
let stack = [
    {
        match:true,
        handle(next){
            setTimeout(() => {
                next(stack)
                console.log('1, done');
            }, 5000);
           
        }
    },
    {
        match:true,
        handle(next){
            next();
            console.log('2,done');
        }
    }
]
function next(stack) {
    if(index === stack.length){
        return
    }
    let item = stack[index++];
    if(item.match){
        item.handle(next)
    }else {
        next(stack)
    }
}

next(stack)

class NextHandler {
    constructor(stack){
        this.stack = stack;
        this.index = 0;
        this.next()
    }
    next(){
        let {index,stack} = this;
        if(index === stack.length){
            return
        }
        let item = stack[index++];
        if(item.match){
            item.handle(next.bind(this))
        }else {
            next.call(this,stack)
        }
    }
}

let handler = new NextHandler(stack)


